package org.medicalinstrument.service;

import net.sf.json.JSONArray;
import org.medicalinstrument.bean.*;
import org.medicalinstrument.dao.ProductDao;
import org.medicalinstrument.dao.RentOrderDao;
import org.medicalinstrument.dao.RentOrderDetailDao;
import org.medicalinstrument.database.DataSourceUtils;
import org.medicalinstrument.impl.ProductDaoImpl;
import org.medicalinstrument.impl.RentOrderDaoImpl;
import org.medicalinstrument.impl.RentOrderDetailDaoImpl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: kongHuan
 * @Date: 2019/3/21 19:59
 * @Description:
 */
public class RentOrderService {
    private String returnString;
    RentOrderDao rentDao = new RentOrderDaoImpl();
    ProductDao proDao = new ProductDaoImpl();
    RentOrderDetailDao detailDao = new RentOrderDetailDaoImpl();

    //提交订单
    public String submitOrder(RentOrderMaster order, List<RentOrderDetail> orderDetails) {
        RentOrderDetailDao detailDao = new RentOrderDetailDaoImpl();
        System.out.println("service-----"+order);
        Connection conn = null;
        try {
            //获得连接
            conn = DataSourceUtils.getConnection();
            //设置事务不自动提交
            conn.setAutoCommit(false);
            DataSourceUtils.startTransaction();
           if(rentDao.add(conn,order)){
               for(RentOrderDetail orderDetail : orderDetails){
                    detailDao.add(conn,orderDetail);
                    //修改订单状态
                   orderDetail.setOrderState(1);
                    detailDao.updateOrderState(conn,orderDetail);
                    //修改商品状态
                    proDao.updateState(conn,1,orderDetail.getProductQR());
               }
               returnString = "insert_success";
               DataSourceUtils.commitAndRelease();
           }
        } catch (Exception e) {
            e.printStackTrace();
            returnString = "insert_fail";
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

        return returnString;
    }

    //获取所有订单信息
    public String getAll() {
        List<RentOrderMaster> rentOrderMasters = null;
        try {
            rentOrderMasters = rentDao.getAll();
            if (rentOrderMasters != null && rentOrderMasters.size() > 0) {
                returnString = JSONArray.fromObject(rentOrderMasters).toString();
            } else {
                returnString = "norentOrderMasters";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }
    //获取指定账户的订单信息
    public String showRentOrder(String accountNo){
        List<RentOrderVo> orders;
        try {
            orders = detailDao.getByAccountNo(accountNo);
            if (orders != null && orders.size() > 0) {
                returnString = JSONArray.fromObject(orders).toString();
            } else {
                returnString = "no_item";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }
    //归还商品
    public String returnProduct(RentOrderDetail detail) {
        Connection conn = null;
        try {
            //获得连接
            conn = DataSourceUtils.getConnection();
            //设置事务不自动提交
            conn.setAutoCommit(false);
            DataSourceUtils.startTransaction();
            if(detailDao.updateRuturnMessage(conn,detail)){
                detail.setOrderState(0);
                detailDao.updateOrderState(conn,detail);
                proDao.updateState(conn, 0,detail.getProductQR());
                DataSourceUtils.commitAndRelease();
                returnString = "return_success";
            }else {
                returnString = "return_fail";
                DataSourceUtils.rollback();
            }
        } catch (SQLException e) {
            returnString = "return_fail";
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return returnString;
    }

    public String showRentOrderDetail(String rentOrderId) {
       List<RentOrderVo> orders;
        try {
            orders = detailDao.getByRentOrderId(rentOrderId);
            if (orders != null && orders.size() > 0) {
                returnString = JSONArray.fromObject(orders).toString();
                //System.out.println(returnString);
            } else {
                returnString = "no_item";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }

    public String searchRentOrderDetail(SearchOption searchOption) {
        List<RentOrderVo> orders;
        try {
            orders = detailDao.getByOption(searchOption);
            if (orders != null && orders.size() > 0) {
                returnString = JSONArray.fromObject(orders).toString();
               // System.out.println(returnString);
            } else {
                returnString = "no_item";
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }
    //申请归还
    public String supplyReturnProduct(RentOrderDetail detail) {
        detail.setOrderState(2);
        try {
            if(detailDao.updateOrderStates(detail)) {
                returnString = "update_success";
            } else {
                returnString = "update_failed";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return returnString;
    }
    public String confirmApply(RentOrderDetail detail) {
        Connection conn = null;
        try {
            //获得连接
            conn = DataSourceUtils.getConnection();
            //设置事务不自动提交
            conn.setAutoCommit(false);
            DataSourceUtils.startTransaction();
            if (detailDao.updateRuturnMessage(conn, detail)) {
                detail.setOrderState(3);
                detailDao.updateOrderState(conn, detail);
                //proDao.updateState(conn, 0,detail.getProductQR());
                DataSourceUtils.commitAndRelease();
                returnString = "supply_return_success";
            } else {
                returnString = "supply_return_fail";
                DataSourceUtils.rollback();
            }
        } catch (SQLException e) {
            returnString = "supply_return_fail";
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return returnString;
    }

    public String confirmReturn(RentOrderDetail detail) {
        Connection conn = null;
        try {
            //获得连接
            conn = DataSourceUtils.getConnection();
            //设置事务不自动提交
            conn.setAutoCommit(false);
            DataSourceUtils.startTransaction();
            detail.setOrderState(0);
            detailDao.updateOrderState(conn,detail);
            proDao.updateState(conn, 0, detail.getProductQR());
            DataSourceUtils.commitAndRelease();
            returnString = "update_success";

        } catch (SQLException e) {
            returnString = "update_failed";
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return returnString;

    }
}
